package com.leo;

import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @ClassName FrostedGlassDemo
 * @Description
 * @Author wangss
 * @date 2020.11.12 12:18
 * @Version 1.0
 */
public class FrostedGlassDemo extends Application {

    private volatile boolean showFlag = false;
    private ImageView iv_blur;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Button show = new Button("展示");
        Button hide = new Button("隐藏");
        ImageView iv = new ImageView("images/2.jpg");

        AnchorPane pane = new AnchorPane();
        pane.getChildren().addAll(iv, show, hide);
        AnchorPane.setLeftAnchor(show, 300d);
        AnchorPane.setLeftAnchor(hide, 400d);

        AnchorPane root = new AnchorPane();

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(1000);
        primaryStage.setHeight(500);
        primaryStage.centerOnScreen();
        primaryStage.setTitle("毛玻璃效果");
        primaryStage.show();

        StackPane view = this.getView(primaryStage);
        root.getChildren().addAll(pane, view);

        iv.fitWidthProperty().bind(primaryStage.widthProperty());
        iv.fitHeightProperty().bind(primaryStage.heightProperty());

        TranslateTransition tt = new TranslateTransition(Duration.seconds(1), view);
        tt.setInterpolator(Interpolator.EASE_OUT);

        show.setOnAction(event -> {
            if (!showFlag) {
                tt.setFromX(-view.getWidth());
                tt.setToX(0);
                tt.play();
            }
        });

        hide.setOnAction(event -> {
            if (showFlag) {
                tt.setFromX(0);
                tt.setToX(-view.getWidth());
                tt.play();
            }
        });

        tt.setOnFinished(event -> {
            showFlag = !showFlag;
        });

        view.translateXProperty().addListener((observable, oldValue, newValue) -> {
            int w = (int) (view.getPrefWidth() + newValue.intValue());
            int h = (int) primaryStage.getHeight();

            if (w > 0) {
                WritableImage wi = new WritableImage(w, h);
                pane.snapshot(new SnapshotParameters(), wi);
                iv_blur.setImage(wi);
            }
        });

        primaryStage.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                // 有个小bug
                if (!showFlag) {
                    view.setTranslateX(-newValue.doubleValue());
                }
            }
        });
    }

    public StackPane getView(Stage stage) {
        StackPane sp = new StackPane();

        // 阴影效果
        DropShadow ds = new DropShadow();
        ds.setRadius(5);
        ds.setColor(Color.valueOf("#A3A3A399"));
        ds.setOffsetX(1);
        sp.setEffect(ds);

        AnchorPane ap = new AnchorPane();
        // 毛玻璃
        iv_blur = new ImageView();
        iv_blur.setEffect(new GaussianBlur(50));
        AnchorPane.setRightAnchor(iv_blur, 0.0);
        ap.getChildren().add(iv_blur);

        // 裁剪
//        iv_blur.setClip();

        VBox vBox = new VBox(20);
        vBox.prefWidthProperty().bind(stage.widthProperty().multiply(0.25));
        vBox.prefHeightProperty().bind(stage.heightProperty());
        vBox.setStyle("-fx-background-color: #ffffff55");
        vBox.setBorder(new Border(new BorderStroke(Color.valueOf("#45454555"), BorderStrokeStyle.SOLID, null, new BorderWidths(1))));

        sp.getChildren().addAll(ap, vBox);

        for (int i = 0; i < 5; i++) {
            vBox.getChildren().add(new Label("javafx demo " + i));
        }

        sp.setTranslateX(-0.25 * stage.getWidth());


        return sp;
    }
}
